﻿@using Newtouch.Infrastructure;
@using Newtouch.HIS.Web.Core.Extensions;
@{
    ViewBag.Title = "InventoryQuery";
    Layout = "~/Views/Shared/_Index.cshtml";
    var reportUrl = Newtouch.Core.Common.Utils.ConfigurationHelper.GetAppConfigValue("ReportUrl");
    var reportSystemCode = Newtouch.Core.Common.Utils.ConfigurationHelper.GetAppConfigValue("ReportSystemCode");
}
<style>
    #btn_search {
        width: 45px;
        font-weight: bolder;
    }

    #btn_print {
        width: 45px;
        background-color: #fff;
        color: #00a9ea;
        font-weight: bolder;
    }

    #TabGrid {
        width: 100%;
        position: fixed;
        bottom: 10px;
        right: 0;
        z-index: 8888;
        background-color: #253443;
        filter: alpha(Opacity=95);
        -moz-opacity: 0.95;
        opacity: 0.95;
        box-shadow: 0px 2px 10px #909090;
    }

        #TabGrid > * {
            filter: alpha(Opacity=95);
            -moz-opacity: 0.95;
            opacity: 0.95;
        }

    .showbotton {
        display: none;
        position: fixed;
        bottom: 5px;
        right: 0;
        z-index: 9999;
        background-color: #ccc;
        width: 50px;
        height: 44px;
        filter: alpha(Opacity=90);
        -moz-opacity: 0.9;
        opacity: 0.9;
        cursor: pointer;
        border-radius: 100px 0 0 100px;
    }
</style>
<form id="form1">
    <div class="panel panel-default" style="margin-bottom:5px;">
        <div class="panel-heading">
            查询条件
        </div>
        <div>
            <table class="form">
                <tr>
                    <th class="formTitle" style="width: 85px;">关键字：</th>
                    <td class="formValue" style="width:150px;">
                        <input id="txtINPUTCODE" type="text" class="form-control" placeholder="药品名称" />
                    </td>
                    <th class="formTitle">库存状态：</th>
                    <td class="formValue" style="width:80px;">
                        @Html.DropDownList("ddlKCZT", EnumKCZT.Disabled.ToDescSelectList(), "全部", new { @class = "form-control" })
                    </td>
                    <th class="formTitle">本部门状态：</th>
                    <td class="formValue" style="width:80px;">
                        @Html.DropDownList("ddlBBMZT", BenBuMenZT.Control.ToDescSelectList(), "全部", new { @class = "form-control" })
                    </td>
                    <td class="formValue" style="width:115px;">
                        <div class="ckbox ckmargin">
                            <input id="chk_NoShow0KC" type="checkbox" /><label for="chk_NoShow0KC">不显示零库存</label>
                        </div>
                    </td>
                    <td class="formTitle" style="width: 240px;">
                        <input type="button" id="btn_search" class="btn btn-md btn-primary" value="查询" />
                        <input type="button" id="btn_print" class="btn btn-md btn-primary" value="打印" />
                        <input type="button" id="btn_expiredDrugsSearch" class="btn btn-md btn-primary" value="过期药品查询" />
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="panel panel-default" style="margin-bottom: 5px;">
        <div class="panel-heading">
            <span>药品总量 > 当前页 > </span>
            <span id='yeCon'>零价总额：" + "元</span>
        </div>
        <div class="gridPanel" id="div-main">
            <table id="gridYPZL"></table>
            <div id="gridPager" rel="form1"></div>
        </div>
    </div>
    <div id="TabGrid">
        <ul class="nav nav-tabs navb-bg" role="tablist" id="myTab">
            <li role="presentation" class="active">
                <a href="#dv_mx" id="linkSearch" role="tab" data-toggle="tab">库存明细</a>
            </li>
            <li style="float:right">
                <span class="hiding glyphicon glyphicon-chevron-right btn-lg"></span>
            </li>
        </ul>
        <div id="dv_mx">
            <table id="gridYPMXInfo"></table>
            <div id="gridMxPager" rel="form1"></div>
        </div>
    </div>
    <div class="showbotton">
        <span class="glyphicon glyphicon-chevron-left btn-lg"></span>
    </div>
</form>
<script>
    var $gridYpzlData = $("#gridYPZL");
    var $gridkcmxData = $("#gridYPMXInfo");
    $(function () {
        $(window).resize(function () {//浏览器窗口调整大小时重新加载jqGrid的宽
            window.initLayout("MyTabGrid");
        });

        $(".hiding,.rows,#dv_search,.toolbar,.panel-default").click(function () {
            TabGridHide();
        });
        $(".panel-default").trigger("click");
        $(".showbotton").click(function () {
            $(".showbotton").animate({ width: 'hide' },
                "fast",
                function () {
                    $("#TabGrid").animate({ left: '0px' }, "slow");
                });
        });
        gridYPZLData();

        $("#btn_search").click(function () {
            $("#yeCon").text("零价总额：__元");
            //每次查询先清空Grid
            $('#gridYPZL').jqGrid("clearGridData");
            $('#gridYPMXInfo').jqGrid("clearGridData");

            $gridYpzlData.jqGrid('setGridParam', {
                url: '/StorageManage/DrugStockSearch/DrugAndStockSearchByPage?r=' + Math.random(),
                postData: getSearchPostData()
            }).trigger('reloadGrid');
            ComputePjzeAndLjze();//计算标题栏中的批价总额和零价总额
        });


        //打印单按钮事件
        $("#btn_print").click(function () {
            var is0kc = $("#chk_NoShow0KC").is(":checked") ? "0" : "-1";
            var kczt = $("#ddlKCZT").val() === '' ? "-1" : $("#ddlKCZT").val();
            var bmzt = $("#ddlBBMZT").val() === '' ? "-1" : $("#ddlBBMZT").val();
            var orgId = '@ViewBag.OrganizeId';
            var bmCode = '@ViewBag.bmCode';
            var uri = '@reportUrl' + "?tempCode=53" + "&systemCode=" + '@reportSystemCode' + "&hospitalCode=" + orgId + "&bmcode=" + bmCode + "&key=" + $("#txtINPUTCODE").val() + "&kczt=" + kczt + "&bmzt=" + bmzt + "&Show0Kc=" + is0kc;
            window.open(uri);
            //window.open("/Report/PrintReport?type=StockQuery&param=bmcode=@ViewBag.bmCode|OrganizeId=@ViewBag.OrganizeId|key=" + $("#txtINPUTCODE").val() + "|kczt=" + kczt + "|bmzt=" + bmzt + "|Show0Kc=" + is0kc, "height=500, width=1195, top=100, left=50, toolbar=no, menubar=no, scrollbar=yes, resizeble=no, location=no, status=no");
        });
        $("#btn_expiredDrugsSearch").click(function() {
            top.top.$.Newtouchtab.tryCloseTab({ name: "过期药品查询", url: "/StorageManage/DrugStockSearch/ExpiredDrugsView" });
            top.top.$.Newtouchtab.addTabWithOutMenu({ name: "过期药品查询", enName: "", url: "/StorageManage/DrugStockSearch/ExpiredDrugsView" });
        });
    });

    $(document).keyup(function (event) {
        if (event.keyCode === 13) {
            $("#btn_search").trigger("click");
        }
    });

    //隐藏搜索框并显示箭头
    function TabGridHide() {
        var tabGridwith = $("#TabGrid").width() + 200;
        if ($(".showbotton").is(":hidden")) {
            $("#TabGrid").animate({ left: tabGridwith },
                "slow",
                function () {
                    $(".showbotton").animate({ width: 'show' }, "fast");
                });
        }
    }

    //查询条件
    function getSearchPostData() {
        var inputCode = $("#txtINPUTCODE").val();
        var kczt = $("#ddlKCZT").val();
        var bbmzt = $("#ddlBBMZT").val();
        var noShow0Kc = $("#chk_NoShow0KC").is(":checked") ? "0" : "1";
        var kcyjcode = "";
		if (sessionStorage.getItem('kcyjyp')) {
			kcyjcode = sessionStorage.getItem('kcyjyp');
			sessionStorage.removeItem('kcyjyp');
		}
		return { keyword: inputCode, tybz: bbmzt, kczt: kczt, show0kc: noShow0Kc, kcyjcode: kcyjcode };
    };

    //单据主记录
    function gridYPZLData() {
        $gridYpzlData.dataGrid({
            height: $(window).height() - document.getElementById('gridYPZL').offsetTop - 75,
            postData: getSearchPostData(),
            url: "/StorageManage/DrugStockSearch/DrugAndStockSearchByPage",
            colModel: [
                { label: '药品名称', name: 'ypmc', width: 150, align: 'left' },
                { label: '药品代码', name: 'ypdm', width: 60, align: 'left' },
                { label: '规格', name: 'gg', width: 100, align: 'left' },
                { label: '拼音', name: 'py', width: 80, align: 'left' },
                { label: '库存数量', name: 'slStr', width: 70, align: 'right' },
                { label: '零价单价', name: 'lsjdjdw', width: 120, align: 'right' },
                {
                    label: '零价总额', name: 'lsze', width: 120, align: 'right',
                    formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(2) : "0.00";
                    }
                },
                { label: '规格', name: 'gg', width: 150, align: 'left' },
                { label: '生产厂商', name: 'sccj', width: 150, align: 'left' },
                { label: '批发价', name: 'pfj', hidden: true },
                { label: '零售价', name: 'lsj', hidden: true }

            ],
            multiselect: false,
            pager: "#gridPager",
            sortname: 'ypdm desc',
            viewrecords: true,
            ondblClickRow: function (rowid, iRow, iCol, e) {
                $gridkcmxData.jqGrid("clearGridData");//每次双击，先清空明细 再重新加载所有选中行明细
                $gridkcmxData.jqGrid('setGridParam', {
                    postData: { ypdm: $("#gridYPZL").jqGridRowValue().ypdm, kczt: $("#ddlKCZT").val() },
                    url: '/StorageManage/DrugStockSearch/StockGroupByBatchSearchByPage?r=' + Math.random()
                }).trigger('reloadGrid');
                $(".showbotton").click();
            },
            loadComplete: function (data) {
                ComputePjzeAndLjze();//计算标题栏中的批价总额和零价总额
            }
        });
    }

    //计算标题栏中的批价总额和零价总额
    function ComputePjzeAndLjze() {
        var titleJjze = 0;
        var titleLjze = 0;
        var count = $("#gridYPZL").getGridParam('records');
        if (count > 0) {
            var rowIds = $("#gridYPZL").getDataIDs();
            for (var i = 0; i < rowIds.length; i++) {
                var getRowJjze = $("#gridYPZL").getRowData(rowIds[i]).jjze;
                var getRowLjze = $("#gridYPZL").getRowData(rowIds[i]).lsze;
                titleJjze += parseFloat(getRowJjze);
                titleLjze += parseFloat(getRowLjze);
            }
            $("#yeCon").text("零价总额：" + titleLjze.toFixed(2) + "元");
        }
    }

    //药品明细
    $gridkcmxData.dataGrid({
        url: "",
        postData: "",
        height: $(window).height() - document.getElementById('gridYPZL').offsetTop - 90,
        colModel: [
            { label: '库存数量', name: 'kcslStr', width: 80, align: 'right' },
            { label: '冻结数量', name: 'djslStr', width: 80, align: 'right' },
            { label: '进价单位', name: 'jjdjdw', width: 120, align: 'right' },
            {
                label: '进价总额', name: 'jjze', width: 80, align: 'right',
                formatter: function (cellvalue) {
                    return cellvalue ? parseFloat(cellvalue).toFixed(2) : "0.00";
                }
            },
            { label: '零售单价', name: 'lsjdjdw', width: 120, align: 'right' },
            {
                label: '零价总额', name: 'lsze', width: 80, align: 'right',
                formatter: function (cellvalue) {
                    return cellvalue ? parseFloat(cellvalue).toFixed(2) : "0.00";
                }
            },
            { label: '批号', name: 'ph', width: 120, align: 'center' },
            { label: '批次', name: 'pc', width: 120, align: 'center' },
            { label: '有效期', name: 'yxq', width: 70, align: 'center', formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' } },
            {
                label: '状态', name: 'kczt', width: 50, align: 'center', formatter: function (val, options) {
                    return val === "1" ? "<i rowId=" + options.rowId + " onclick='operateZt(this)' class=\"fa fa-toggle-on\"></i>" : "<i rowId=" + options.rowId + " onclick='operateZt(this)' class=\"fa fa-toggle-off\"></i>";
                }
            },
            { label: '冻结数量', name: 'djsl', hidden: true },
            { label: 'ypdm', name: 'ypdm', hidden: true },
            { label: '转化因子', name: 'zhyz', hidden: true},
            { label: '库位', name: 'ypkw', hidden: true }
        ],
        multiselect: false,
        pager: "#gridMxPager",
        viewrecords: true,
        sortname: 'yxq asc'
    });

    //设置库存有效与否
    function operateZt(obj) {
        var zt = "1";
        var ph = $gridkcmxData.getRowData($(obj).attr("rowId")).ph;
        var pc = $gridkcmxData.getRowData($(obj).attr("rowId")).pc;
        var ypdm = $gridkcmxData.getRowData($(obj).attr("rowId")).ypdm;
        if ($(obj).hasClass("fa-toggle-off")) {
            $(obj).removeClass("fa-toggle-off").addClass("fa-toggle-on");
            zt = "1";
        } else {
            $(obj).removeClass("fa-toggle-on").addClass("fa-toggle-off");
            zt = "0";
        }

        $.najax({
            url: "/StorageManage/DrugStockSearch/UpdateKcxxZt",
            type: "POST",
            data: { ypdm: ypdm, ph: ph, pc: pc, zt: zt },
            dataType: "json",
            success: function (data) {
                if (data.state === 'success') {
                }
                else {
                    $.modalMsg(data, "error", 1000);
                }
            }
        });
    }
</script>


